﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace DoAn
{
    public class Map : VisibleGameEntity
    {

        protected float _scale = 1.0f;

        public float Scale
        {
            get { return _scale; }
            set { _scale = value; }
        }

        protected Vector2 _defaultSize;

        public Vector2 DefaultSize
        {
            get { return _defaultSize; }
            set { _defaultSize = value; }
        }

        public Map(ContentManager content, string pic, Vector2 topleft, Vector2 size)
        {
            Topleft = topleft;
            Size = size;
            DefaultSize = Size;

            Texture2D[] texture = new Texture2D[1];
            texture[0] = content.Load<Texture2D>(pic);

            mySprites = new List<MySprite>();
            MySprite temp = new MySprite(texture, topleft);
            mySprites.Add(temp);
            nSprites = 1;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Rectangle rect = new Rectangle((int)Topleft.X, (int)Topleft.Y, (int)Size.X, (int)Size.Y);
            mySprites[0].DrawMap(gameTime, spriteBatch, rect);
        }

        public void ScrollMap(MouseState mouseState, int viewWidth, int viewHeight)
        {
            Vector2 tempmap = Topleft;
            if (mouseState.X < 20)
            {
                tempmap.X += 10;
            }
            if (mouseState.X > viewWidth - 56)
            {
                tempmap.X -= 10;
            }
            if (mouseState.Y < 20)
            {
                tempmap.Y += 10;
            }
            if (mouseState.Y > viewHeight - 50)
            {
                tempmap.Y -= 10;
            }
            
            Topleft = tempmap;

            Vector2 vt = Topleft;
            if (Topleft.X < -Size.X + viewWidth - 5)
                vt.X = -Size.X + viewWidth - 5;
            if (Topleft.Y < -Size.Y + viewHeight - 5)
                vt.Y = -Size.Y + viewHeight - 5;
            if (Topleft.X > 5)
                vt.X = 5;
            if (Topleft.Y > 5)
                vt.Y = 5;
            Topleft = vt;
        }

        public void ScrollMap(KeyboardState kbState, int viewWidth, int viewHeight)
        {
            Vector2 ufo = Topleft;
            if (kbState.IsKeyDown(Keys.Left) && ufo.X < 0)
            {
                ufo.X += 10;
                MoveAll(new Vector2(10, 0));
            }
            if (kbState.IsKeyDown(Keys.Right) && ufo.X > (viewWidth - Size.X))
            {
                ufo.X -= 10;
                MoveAll(new Vector2(-10, 0));
            }
            if (kbState.IsKeyDown(Keys.Up) && ufo.Y < 0)
            {
                ufo.Y += 10;
                MoveAll(new Vector2(0, 10));
            }
            if (kbState.IsKeyDown(Keys.Down) && ufo.Y > viewHeight - Size.Y)
            {
                ufo.Y -= 10;
                MoveAll(new Vector2(0, -10));
            }
            Topleft = ufo;
        }
        public void MoveAll(Vector2 vt)
        {
            for (int i = 0; i < Game1.init.nSprites; i++)
            {
                Game1.init.mySprites[i].Position += vt;
                Game1.init.mySprites[i].Target = Game1.init.mySprites[i].PosCenter;
            }
        }
    }
}